package com.gxuwz.stats.listener;

import com.gxuwz.stats.entity.TotalWork;
import com.gxuwz.stats.entity.TotalWorkExcelDTO;
import com.gxuwz.stats.entity.User;
import com.gxuwz.stats.service.ExcelDataConverter;
import com.gxuwz.stats.service.TotalWorkService;
import com.gxuwz.stats.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
public class TotalWorkExcelListener extends BaseExcelListener<TotalWorkExcelDTO> {
    private final TotalWorkService totalWorkService;
    private final ExcelDataConverter converter;

    public TotalWorkExcelListener(UserService userService,
                                  TotalWorkService totalWorkService,
                                  ExcelDataConverter converter) {
        super(userService);
        this.totalWorkService = totalWorkService;
        this.converter = converter;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    protected void saveData() {
//        for (TotalWorkExcelDTO dto : dataList) {
//            try {
//                // 获取或创建用户
//                User user = converter.convertToUser(dto);
//                String username = dto.getUsername();
//                String department = dto.getDepartment();
//                if (username == null || username.trim().isEmpty() || department == null || department.trim().isEmpty()) {
//                    System.err.println("Skipping invalid record due to missing username or department.");
//                    continue;
//                }
//                userService.saveOrUpdate(user);
//
//                // 保存总工作量
//                TotalWork totalWork = converter.convertToTotalWork(dto, user.getUserId());
//                totalWorkService.save(totalWork);
//            } catch (Exception e) {
//                log.error("保存总工作量数据失败: {}", dto, e);
//                throw new RuntimeException("数据保存失败", e);
//            }
//        }
    }
}
